package com.premadefood.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.premadefood.common.BaseResponse;
import com.premadefood.common.ErrorCode;
import com.premadefood.domain.News;
import com.premadefood.exception.BusinessException;
import com.premadefood.service.NewfocusService;
import com.premadefood.service.NewsService;
import com.premadefood.service.UserService;
import com.premadefood.utils.ResultUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Resource
    private NewsService newsService;


    @GetMapping("/recommend")
    public BaseResponse<Page<News>> newsRecommend(long pageNum) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();

        Page<News> newsPage = newsService.page(new Page<>(pageNum, 4), queryWrapper);

        return ResultUtils.success(newsPage);
    }

    @GetMapping("/Detail")
    public BaseResponse<News> newsDetail(Integer newsId,HttpServletRequest request) {
        if (newsId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "资讯id错误");
        }
        News news = newsService.getDetail(newsId,request);
        return ResultUtils.success(news);
    }

//    @PostMapping("/follow")
//    public BaseResponse<Boolean> followAuthor(String authorName){
//
//    }


    /**
     * 查看观看历史
     *
     * @return
     */
    @GetMapping("/history")
    public BaseResponse<List<News>> viewedHistoryNews(HttpServletRequest request) {
        List<News> newsList = newsService.viewedHistoryNews(request);
        return ResultUtils.success(newsList);
    }

    /**
     * 查看关注了的作者以及该作者发布过的咨询内容
     */
    @GetMapping("/focus")
    public BaseResponse<List<News>> focusNews(HttpServletRequest request) {
        List<News> newsList = newsService.getFocusNews(request);
        //newsList中包含了作者字段
        return ResultUtils.success(newsList);

    }

    /**
     * 根据标题查询资讯
     *
     * @param searchContext
     * @return
     */
    @GetMapping("/search")
    public BaseResponse<List<News>> searchNews(String searchContext) {
        if (StringUtils.isBlank(searchContext)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "搜索参数为空");
        }
        List<News> newsList = newsService.searchNewsByTitle(searchContext);
        return ResultUtils.success(newsList);
    }


}
